package nl.haemers.galleries.controls
{
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	//import nl.haemers.view.components.buttons.ItemButton;
	import nl.haemers.galleries.events.GallerycontrolEvent;
	
	import nl.haemers.base.BaseSprite;
	
	/**
	 * @author Jonathan Haemers
	  */
	public class ControlContainer extends BaseSprite
	{		
		protected var previousButton				:Sprite;
		protected var nextButton					:Sprite;
		//protected var itemButton					:ItemButton;				
		//protected var itemButtons					:Vector.<ItemButton>;
		
		private var _selectedItemId					:int;
		private var _totalItems						:int;
		protected var offsetItems					:int = 15;
		
		public function ControlContainer() 
		{
			super();
		}
		
		/** Public methods **/
		public function show() : void
		{
			
		}
		
		public function hide() : void
		{
			
		}
		/** Getters & Setters **/
		public function get selectedItemId() : int
		{ 
			return _selectedItemId;
		}
		public function set selectedItemId( value:int ) : void 
		{
			_selectedItemId = value;
		}
		
		public function set totalItems( value:int ) : void 
		{
			_totalItems = value;
			if (stage && !itemButtons)
			{
				createButtons();
				configButtons();
			}
			else if (stage)
			{
				addButton();
			}
		}
		public function get totalItems() : int
		{ 
			return _totalItems;
		}
		
		/** Protected methods **/
		override protected function init( e:Event = null ) : void
		{
			removeEventListener(Event.ADDED_TO_STAGE, init);
			if (!itemButtons)
			{
				createButtons();
				configButtons();
			}
		}
		
		protected function configButtons() : void
		{
			if (previousButton)
			{
				previousButton.addEventListener(MouseEvent.CLICK, previousHandler);
				nextButton.addEventListener(MouseEvent.CLICK, nextHandler);
			}
		}
		
		protected function previousHandler(e:MouseEvent) : void
		{
			changeSelectedItem(_selectedItemId--);
			dispatchEvent(new GallerycontrolEvent(GallerycontrolEvent.PREVIOUS_ITEM));
		}
		
		protected function nextHandler(e:MouseEvent) : void
		{
			changeSelectedItem(_selectedItemId++);
			dispatchEvent(new GallerycontrolEvent(GallerycontrolEvent.NEXT_ITEM));
		}
		
		protected function itemHandler(e:MouseEvent) : void
		{
			var _selectedItem:ItemButton = e.currentTarget as ItemButton;
			changeSelectedItem(_selectedItem.id);
			dispatchEvent(new GallerycontrolEvent(GallerycontrolEvent.ITEM_SELECTED));
		}
		
		protected function changeSelectedItem(index:int) : void
		{
			_selectedItemId = index;
		}
		
		protected function createButtons() : void
		{
			if (_totalItems > 0)
			{
				previousButton = draw(10, 5, 0x00FF00);
				nextButton = draw(10, 5, 0x00FF00);
				
				itemButtons = new Vector.<ItemButton>();
				for (var i:int = 0; i < _totalItems; i++)
				{
					itemButton = new ItemButton();
					itemButton.id = i;
					itemButton.addEventListener(MouseEvent.CLICK, itemHandler);
					addChild(itemButton);
					
					itemButtons[i] = itemButton;
				}
				
				addChild(previousButton);
				addChild(nextButton);
				
				positioncontrols();
			}
		}
		
		protected function addButton() : void
		{
			itemButton = new ItemButton();
			itemButton.id = itemButtons.length-1;
			itemButton.addEventListener(MouseEvent.CLICK, itemHandler);
			addChild(itemButton);
			
			itemButtons.push(itemButton);
			
			positioncontrols();
		}
		
		protected function positioncontrols() : void
		{
			previousButton.x = 0;
			var offsetX:int = (width - (itemButtons.length * offsetItems))*.5;
			
			for (var i:int = 0; i < itemButtons.length; i++)
			{
				itemButtons[i].x = (offsetItems * i) + offsetX;
			}
			nextButton.x = width - nextButton.width;
		}
		
		protected function draw(rad:Number, hRad:Number, color:uint) : Sprite
		{
			var spr:BaseSprite = new BaseSprite();
			spr.graphics.beginFill(color);
			spr.graphics.drawCircle(0, 0, rad);
			spr.graphics.endFill();
			return spr;
		}
	}

}